1549B - Gregor and the Pawn Game - CodeForces Solution


dfs and similar dp flows graph matchings graphs greedy implementation *800

Please click on ads to support us..

Python Code:

import math
def intlist():
    return([int(i) for i in input().split(" ")])
def stringlist():
    return (input().split(" "))
def yes(t):
    if t:
        return("YES")
    return("NO")
def printjoin(l,str=False):
    if str:
        l=[str(i) for i in l]
    return(" ".join(l))
def power_of_two(n):
    b=bin(n)[2:]
    return(int(b)==10**(len(b)-1))
def bit_count(n):
    return(bin(n).count('1'))
def gcd(x, y):
    while(y):
       x, y = y, x % y
    return x
def lcm(x, y):
    lcm = (x*y)//gcd(x,y)
    return lcm
def isint(str):
    try:
        int(str)
        return True
    except ValueError:
        return False
def solve():
    n=int(input())
    a=list(input())
    b=list(input())
    ans=0
    for i in range(n):
        if b[i]!='1':
            continue
        if i!=0 and a[i-1]=='1':
            a[i-1]="2"
            ans+=1
        elif a[i]=='0':
            a[i]=='2'
            ans+=1
        elif i!=n-1 and a[i+1] == "1":
            a[i+1]="2"
            ans+=1
    return(ans)
t = input()
for tt in range(int(t)):
    print(solve())

C++ Code:

  #include<bits/stdc++.h>
  #define ll long long

  using namespace std;

  int main()
  {
      ios::sync_with_stdio(0);
      cout.tie(0);
      cin.tie(0);

      int n, x; cin >> n;
      string s, s2;
      int result[n] = {0};
      for(int i = 0; i < n; i++)
      {
          cin >> x >> s >> s2;
          bool reach[x] = {false};
          for(int j = 0; j < x; j++)
          {
            if(s2[j] == '1')
              {
                  if(s[j] == '0' && reach[j] == false)
                  {
                    result[i]++;
                    reach[j] = true;
                  }
                  else
                  {
                      if(j == 0)
                      {
                        if(s[j+1] == '1' && reach[j+1] == false)
                        {
                          result[i]++;
                          reach[j+1] = true;
                        }
                      }
                      else
                      {
                          if(s[j-1] == '1' && reach[j-1] == false)
                          {
                              result[i]++;
                              reach[j-1] = true;
                          }
                          else if(j != x-1)
                          {
                              if(s[j+1] == '1' && reach[j+1] == false)
                              {
                                  result[i]++;
                                  reach[j+1] = true;
                              }
                          }

                      }

                  }
              }
          }
      }

      for(auto i : result)
        cout << i << endl;

      return 0;
  }



Comments

Submit
0 Comments
More Questions

855B - Marvolo Gaunt's Ring
1454A - Special Permutation
1359A - Berland Poker
459A - Pashmak and Garden
1327B - Princesses and Princes
1450F - The Struggling Contestant
1399B - Gifts Fixing
1138A - Sushi for Two
982C - Cut 'em all
931A - Friends Meeting
1594A - Consecutive Sum Riddle
1466A - Bovine Dilemma
454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push
706B - Interesting drink
1265A - Beautiful String
214A - System of Equations
287A - IQ Test
1108A - Two distinct points
1064A - Make a triangle
1245C - Constanze's Machine
1005A - Tanya and Stairways
1663F - In Every Generation
1108B - Divisors of Two Integers
1175A - From Hero to Zero
1141A - Game 23